package com.glorysoft.sunnypda.ui.reportrepair

import androidx.databinding.ObservableArrayList
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.base.Constant
import com.glorysoft.sunnypda.entity.ActionEntity
import com.glorysoft.sunnypda.entity.RepairInfoEntity
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.glorysoft.sunnypda.model.RepairRepository
import com.google.gson.JsonObject
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: ReportRepairInfoViewModel
 * Author: XJH
 * Date: 2022/1/7 16:54
 * Description:
 * History:
 */
class ReportRepairInfoViewModel : BaseViewModel() {

    val repairRepository : RepairRepository by lazy { RepairRepository() }
    var repaidInfoEntity = RepairInfoEntity()
    val priorityList = arrayListOf("低","中","高")
    val userList = ArrayList<String>()
    val typeList = ArrayList<String>()

    var actionItems = ObservableArrayList<ActionEntity>()
    var actionBinding =
        ItemBinding.of<ActionEntity>(BR.actionItem, R.layout.item_repair_action)

    fun getRepairInfo(id : String){
        val map = JsonObject()
        map.addProperty("RFR_ID",id)
        launchOnlyResult({
            repaidInfoEntity = repairRepository.getRepairInfo(map)
            for ((index,e) in repaidInfoEntity.ACTIONLIST.withIndex()) {
                e.POSITION = (index + 1).toString()
            }
            actionItems.clear()
            actionItems.addAll(getMaoPaoPaiXuList(repaidInfoEntity.ACTIONLIST))
            getTypeInfo(repaidInfoEntity.EQP_ID)
            defUI.callObserve.value = CallObserveBean(0)
        })
    }

    fun getUserInfo(){
        val map = JsonObject()
        launchOnlyResult({
            val userListInfo = repairRepository.getUserInfo(map)
            defUI.callObserve.value = CallObserveBean(1,userListInfo)
        })
    }
    fun getTypeInfo(qepID:String){
        val map = JsonObject()
        map.addProperty("EQPID",qepID)
        launchOnlyResult({
            val typeListInfo = repairRepository.getTypeInfo(map)
            typeListInfo.forEach{
                typeList.add(it.faulT_TYPE)
            }
        })
    }


    fun changeRepairStatus(status : Int,user:String,time:String?,remark:String){
        val map = JsonObject()
        map.addProperty("RFR_ID",repaidInfoEntity.RFR_ID)
        map.addProperty("RFRSTATUS",status)
        map.addProperty("MAINTAIN_USER",Constant.CURRENT_USER)
        map.addProperty("REPAIR_DES",repaidInfoEntity.REPAIR_DES)
        map.addProperty("RPR_START_TIME",if (status == 1) FreeApi_DateUtils.getNowStringDate("yyyy-MM-dd HH:mm:ss") else "")
        map.addProperty("RPR_END_TIME",if (status == 2) FreeApi_DateUtils.getNowStringDate("yyyy-MM-dd HH:mm:ss") else "")
        map.addProperty("ISquality",repaidInfoEntity.ISQUALITY)
        map.addProperty("PRIORITY",repaidInfoEntity.PRIORITY)
        map.addProperty("RFR_TYPE",repaidInfoEntity.RFR_TYPE)
        map.addProperty("RPR_INPUT",repaidInfoEntity.RPR_INPUT)
        map.addProperty("EQP_ID",repaidInfoEntity.EQP_ID)
        map.addProperty("LOT_ID",repaidInfoEntity.LOT_ID)
        map.addProperty("EQP_TYPE",repaidInfoEntity.EQP_TYPE)
        map.addProperty("SHIFT",repaidInfoEntity.SHIFT)
        map.addProperty("REPAIR_DES",repaidInfoEntity.REPAIR_DES)
        map.addProperty("REVICE_USER",user)
        map.addProperty("FINISH_TIME",time)
        map.addProperty("ISSUE_REMARK",remark)
        launchOnlyResult({
            repairRepository.changeRepairStatus(map)
            defUI.startActivity.call()
        })
    }

    @JvmName("getRepairAllList1")
    fun getMaoPaoPaiXuList(list:List<ActionEntity>): List<ActionEntity> {
        val repairList  = ArrayList<ActionEntity>()
        val id = ArrayList<Int>()
        for (i in 0..list.size-1){
            val idi = list[i].POSITION.toInt()
            id.add(idi)
        }
        for (i in 1..id.size){
            for (j in 0..id.size-2){
                if (id[j] > id[j+1]){
                    val sort = id[j]
                    id[j] = id[j+1]
                    id[j+1] = sort
                }
            }
        }
        for (i in id) {
            list.forEach {
                val rfr_id = it.POSITION.toInt()
                if (i == rfr_id){
                    repairList.add(it)
                }
            }
        }

        return repairList
    }

}